package com.socool.sinofaith.ui;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.adapter.QuestionSubTaskAdapter;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.SubTaskQuestionMessage;
import com.socool.sinofaith.model.TaskQuestionMessage;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.SinofaithUtil;
import com.socool.sinofaith.view.LoadingDialog;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 舍弃
 */
public class QuestionSubTaskActivity extends BaseActivity implements
		CenterViewFactory, SpinnerViewFactory {

	ActionBarHelperBase mActionBarHelperBase;
	private String TaskName;
	private QuestionSubTaskAdapter mAdapter;
	private String TaskID;
	private ListView listView;
	private LoadingDialog mLoadingDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SocoolApplication.addActivity(this);
		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);
		setContentView(R.layout.activity_question_subtask);
		listView = (ListView) findViewById(R.id.lv_question_subtask);
		mAdapter = new QuestionSubTaskAdapter(this);
		TaskName = getIntent().getStringExtra("TaskName");
		TaskID = getIntent().getStringExtra("TaskID");
		mLoadingDialog = new LoadingDialog(this);
		getData();
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				
			}
		});

	}

	@Override
	public View createSpinnerView() {
		ImageView imageview = new ImageView(this);
		return imageview;
	}

	@Override
	public View createView() {
		TextView tv = new TextView(this);
		tv.setText(TaskName);
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		tv.setSingleLine(true);
		tv.setMaxEms(10);
		tv.setEllipsize(TruncateAt.END);
		return tv;
	}

	@SuppressWarnings("unchecked")
	private void getData() {
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.completeTask_url);
		params.put("did", SinofaithUtil.did);
		params.put("mobile", getSPString());
		params.put("taskid", TaskID);

		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal
				.getTime());
		Log.v("qqqq", yesterday);
		params.put("date", yesterday);

		fh.get(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack() {
			@Override
			public void onStart() {
				super.onStart();
				mLoadingDialog.show();
			}

			@Override
			public void onLoading(long count, long current) {
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				mLoadingDialog.dismiss();
				super.onSuccess(t);
				// Toast.makeText(YesterdayFinishFragment.this.getActivity(),
				// t+"", Toast.LENGTH_LONG).show();
				try {

					SubTaskQuestionMessage message = HttpJsonAdapter
							.getInstance().get(t.toString(),
									SubTaskQuestionMessage.class);
					if ("0".equals(message.getResult())) {
						mAdapter.setDatas(message.getBean());
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				mLoadingDialog.dismiss();
				super.onFailure(t, errorNo, strMsg);
				Toast.makeText(QuestionSubTaskActivity.this, strMsg,
						Toast.LENGTH_LONG).show();
			}

		});

	}
	private String getSPString(){
		SharedPreferences sp=this.getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}
}
